package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.logic.DbInfoParser;
import com.yanqu.road.dao.impl.logic.ProcessInfoParser;
import com.yanqu.road.entity.servercenter.CrossRankServerInfo;
import com.yanqu.road.entity.servercenter.CrossServerInfo;
import com.yanqu.road.entity.servercenter.DbInfo;
import com.yanqu.road.entity.servercenter.ProcessInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CrossServerInfoDaoImpl extends TempDao {
    public CrossServerInfo getCrossServerInfo(int channelId, int serverId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_cross_server_list where channelId = ? and crossServerId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, channelId);
            call.setInt(2, serverId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossServerInfo crossServerInfo = parseCrossServerInfo(serverId, rs);
                return crossServerInfo;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public CrossServerInfo getCrossServerInfo(int crossServerId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_cross_server_list where crossServerId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, crossServerId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossServerInfo crossServerInfo = parseCrossServerInfo(crossServerId, rs);
                return crossServerInfo;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public List<CrossServerInfo> getAllChannelCrossServerInfo(int channelId) {
        List<CrossServerInfo> crossServerInfoList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_cross_server_list where channelId = ? ";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                int serverId = rs.getInt("crossServerId");
                CrossServerInfo crossServerInfo = parseCrossServerInfo(serverId, rs);
                crossServerInfoList.add(crossServerInfo);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return crossServerInfoList;
    }

    private CrossServerInfo parseCrossServerInfo(int serverId, ResultSet rs) throws SQLException {
        CrossServerInfo crossServerInfo = new CrossServerInfo();
        crossServerInfo.setCrossServerId(serverId);
        DbInfo dbInfo = DbInfoParser.parseDbInfo(rs.getString("crossDatabase"));
        if (dbInfo != null) {
            crossServerInfo.getDbInfos().add(dbInfo);
        }
        DbInfo configbase = DbInfoParser.parseDbInfo(rs.getString("crossConfigbase"));
        if (configbase != null) {
            crossServerInfo.getDbInfos().add(configbase);
        }

        ProcessInfo crossProcessInfo = ProcessInfoParser.parsingProcessInfo(rs.getString("crossServerInfo"), serverId);
        ProcessInfo webServerInfo = ProcessInfoParser.parsingProcessInfo(rs.getString("webServerInfo"), serverId);
        crossServerInfo.getProcessInfoList().add(crossProcessInfo);
        crossServerInfo.getProcessInfoList().add(webServerInfo);
        crossServerInfo.setCrossServerType(rs.getInt("crossServerType"));
        return crossServerInfo;
    }

    public CrossRankServerInfo getCrossRankServerId(long serverId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_server_cross_rank_server where serverId  = ?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, serverId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parseCrossRankServerInfoData(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }


    protected CrossRankServerInfo parseCrossRankServerInfoData(ResultSet rs) throws SQLException {
        CrossRankServerInfo crossRankServerInfo = new CrossRankServerInfo();
        crossRankServerInfo.setServerId(rs.getInt("serverId"));
        crossRankServerInfo.setCrossRankServerId(rs.getInt("crossRankServerId"));
        crossRankServerInfo.setCrossCenterServerId(rs.getInt("crossCenterServerId"));
        crossRankServerInfo.setCrossBanquetServerId(rs.getInt("crossBanquetServerId"));
        return crossRankServerInfo;
    }


}
